#ifndef __APP_DEFS_TYPE_H_
#define __APP_DEFS_TYPE_H_
#ifdef __cplusplus
extern "C"
{
#endif

#include "stdio.h"
//malloc free memset memcpy need the header file
#include<stdlib.h>
#include<string.h>
#include <stddef.h>//size_t
#include <stdbool.h>//bool
//#include <unistd.h> //usleep sleep
#include "debug_log_printf.h"
//#include <time.h>
//#include <sys/time.h>

#pragma pack(4)

#define TYPE_VER				"V01.00"
#define EZSP_HOST				1
//���������չ
#ifdef _TYPE_EXTERN_
#define EXT_TYPE	
#else
#define EXT_TYPE				extern
#endif // _TYPE_EXTERN_

#define osal_memset  	memset
//#define osal_memcpy  	memcpy
#define osal_strlen  	strlen

#define host_malloc 	malloc
#define host_free 		free

#define osal_mem_alloc  host_malloc
#define osal_mem_free   host_free
#define osal_msg_deallocate 	host_malloc
#define MEMSET(d, v, l)  		 memset(d, v, l)
#define MEMCOPY(d, s, l) 		 memcpy(d, s, l)
#define MEMMOVE(d, s, l) 		 memmove(d, s, l)
#define MEMCOMPARE(s0, s1, l) 	 memcmp(s0, s1, l)
#define MEMPGMCOMPARE(s0, s1, l) memcmp(s0, s1, l)

//-----------------------------------------------------------------------------
#ifndef pdTRUE
#define pdTRUE		( 1 )
#endif

#ifndef pdFALSE
#define pdFALSE		( 0 )
#endif

#ifndef pdPASS
#define pdPASS									( 1 )
#endif

#ifndef pdFAIL
#define pdFAIL									( 0 )
#endif

#ifndef errQUEUE_EMPTY
#define errQUEUE_EMPTY							( 0 )
#endif

#ifndef errQUEUE_FULL
#define errQUEUE_FULL							( 0 )
#endif


#ifndef true
#define true					1
#endif // true

#ifndef false
#define false					0
#endif // false

#ifndef TRUE
#define TRUE					1
#endif // true

#ifndef FALSE
#define FALSE					0
#endif // false

// Table 193 -- Logic Values
#ifndef YES
#define YES						1
#endif // YES

#ifndef NO
#define NO						0
#endif // NO

#ifndef SET
#define SET						1
#endif // SET

#ifndef CLEAR
#define CLEAR					0
#endif // CLEAR

#ifndef NULL
#define NULL					((intptr_t)(0))
#endif // NULL

#ifndef Z_EXTADDR_LEN
#define Z_EXTADDR_LEN   		8
#endif

#ifndef SUCCESS
#define SUCCESS                   0x00
#endif

#ifndef  FAILURE
#define FAILURE                   0x01
#endif


#ifndef BV
#define BV(n)      (1 << (n))
#endif

#ifndef BF
#define BF(x,b,s)  (((x) & (b)) >> (s))
#endif

#ifndef MIN
#define MIN(n,m)   (((n) < (m)) ? (n) : (m))
#endif

#ifndef MAX
#define MAX(n,m)   (((n) < (m)) ? (m) : (n))
#endif

#ifndef ABS
#define ABS(n)     (((n) < 0) ? -(n) : (n))
#endif


/* takes a byte out of a uint32_t : var - uint32_t,  ByteNum - byte to take out (0 - 3) */
#define BREAK_UINT32( var, ByteNum ) \
          (uint8_t)((uint32_t)(((var) >>((ByteNum) * 8)) & 0x00FF))

#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \
          ((uint32_t)((uint32_t)((Byte0) & 0x00FF) \
          + ((uint32_t)((Byte1) & 0x00FF) << 8) \
          + ((uint32_t)((Byte2) & 0x00FF) << 16) \
          + ((uint32_t)((Byte3) & 0x00FF) << 24)))

#define BUILD_UINT16(loByte, hiByte) \
          ((uint16_t)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))

#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)

#define BUILD_UINT8(hiByte, loByte) \
          ((uint8_t)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))

#define HI_UINT8(a) (((a) >> 4) & 0x0F)
#define LO_UINT8(a) ((a) & 0x0F)

#ifndef GET_BIT
#define GET_BIT(DISCS, IDX)  (((DISCS)[((IDX) / 8)] & BV((IDX) % 8)) ? TRUE : FALSE)
#endif
#ifndef SET_BIT
#define SET_BIT(DISCS, IDX)  (((DISCS)[((IDX) / 8)] |= BV((IDX) % 8)))
#endif
#ifndef CLR_BIT
#define CLR_BIT(DISCS, IDX)  (((DISCS)[((IDX) / 8)] &= (BV((IDX) % 8) ^ 0xFF)))
#endif

/*
 *  This macro is for use by other macros to form a fully valid C statement.
 *  Without this, the if/else conditionals could show unexpected behavior.
 *
 *  For example, use...
 *    #define SET_REGS()  st( ioreg1 = 0; ioreg2 = 0; )
 *  instead of ...
 *    #define SET_REGS()  { ioreg1 = 0; ioreg2 = 0; }
 *  or
 *    #define  SET_REGS()    ioreg1 = 0; ioreg2 = 0;
 *  The last macro would not behave as expected in the if/else construct.
 *  The second to last macro will cause a compiler error in certain uses
 *  of if/else construct
 *
 *  It is not necessary, or recommended, to use this macro where there is
 *  already a valid C statement.  For example, the following is redundant...
 *    #define CALL_FUNC()   st(  func();  )
 *  This should simply be...
 *    #define CALL_FUNC()   func()
 *
 * (The while condition below evaluates false without generating a
 *  constant-controlling-loop type of warning on most compilers.)
 */
#define st(x)      do { x } while (__LINE__ == -1)

/* This macro is used to define asm NOP instruction with
 * different compiler/assembler
 */
#ifdef ccs
#define ASM_NOP    asm(" NOP")
#elif defined rvmdk
#define ASM_NOP   __nop()
#else
#define ASM_NOP    asm("NOP")
#endif

/**
 * @brief Standard program memory delcaration.
 */
  #define PGM     const

/**
 * @brief Char pointer to program memory declaration.
 */
  #define PGM_P   const char *

/**
 * @brief Unsigned char pointer to program memory declaration.
 */
  #define PGM_PU  const unsigned char *

  #define PGM_NO_CONST
#define TEXT_SIZE  128
#define ZIGBEE_UTCTime				946656000L//2.6.2.22  UTCTime
/**********************************************************************************
			typedef data type
**********************************************************************************/

//typedef unsigned char			BOOL;	///<BOOL
//typedef unsigned char   		bool; in <stdbool.h>
typedef unsigned char			boolean; 

typedef unsigned char			BYTE;
typedef unsigned char 			byte;

typedef	unsigned char			UINT8;	///<unsigned char
typedef	signed char             INT8;	///< char
typedef signed   char   		int8;
typedef signed   char   		int8_t;

typedef unsigned char 			U8;
typedef signed char 			S8;
typedef unsigned char			uint8; 
typedef unsigned char		    uint8_t; 


typedef unsigned short 			U16;
typedef signed short 			S16;
typedef unsigned short			uint16;
typedef	unsigned short			UINT16;	///<unsigned char
typedef	signed short            INT16;	///<short
typedef unsigned short  		uint16_t;
typedef	signed short            int16;
typedef	signed short            int16_t;


typedef signed int 				int24;
typedef unsigned int			uint24;

typedef unsigned int            uint32;
typedef	signed int              int32;
typedef	signed int              int32_t;

typedef unsigned int            UINT32;	///<unsigned int
typedef	signed int              INT32;	///<int
typedef unsigned int 			U32;
typedef signed int 				S32;
typedef unsigned int			uint32_t;


typedef unsigned long long 		UINT64;
typedef unsigned long long 		U64;
typedef unsigned long long 		uint64;
//typedef unsigned long long 		uint64_t;


typedef uint8 					Status_t;

//typedef byte ZLongAddr_t[Z_EXTADDR_LEN];
typedef byte 	MacAddr_t[6];


//typedef uint8 				ZStatus_t;

//typedef uint8				afStatus_t;

typedef struct _version
{
	uint8_t main_version;
	uint8_t sub_version;
	uint8_t fix_version;
	char compileTimeString[20];//"2019 01 16 15:39:55"
}NcpFirmwareVer_t;
typedef struct __doubleReturnValue
{
	uint32 first;
	uint32 last;
}doubleReturnValue_t;


/**********************************************************************************
			extern function
**********************************************************************************/
extern uint8* break_buffer_uint32( uint8 *buf, uint32 val );
extern uint32 build_uint32( uint8 *swapped, uint8 len );
extern uint8*osal_buffer_uint32(uint8 * buf, uint32 val);
extern uint32 osal_build_uint32( uint8 *swapped, uint8 len );


#ifdef __cplusplus
}
#endif
#endif//__APP_DEFS_TYPE_H_
